<?php

namespace App\Common\Services\Store;


use App\Admin\Models\SiteCompany;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Redis;

class Common
{
    public static function getIsStoreAccount(Request $request){
        $accounttype=self::getTokenInfo($request, 'type');
        return !in_array($accounttype,[1,2]);
    }
    /**
     * 获取当前登录人员详情
     */
    public static function getCurrentAccountinfo(Request $request)
    {
        return self::getTokenInfo($request);
    }
    /**
     * 获取当前登录人员公司ID
     */
    public static function getCurrentAccountId(Request $request)
    {
        return self::getTokenInfo($request, 'id');
    }
    /**
     * 获取当前登录人员门店ID
     */
    public static function getCurrentAccountStoreId(Request $request)
    {
        return self::getTokenInfo($request, 'store_ids');
    }
    /**
     * 获取当前登录人员公司ID
     */
    public static function getCurrentAccountCompanyId(Request $request)
    {
        return self::getTokenInfo($request, 'cp_id');
    }
    /**
     * 从token提取信息
     */
    private static function getTokenInfo($request, $key = '')
    {
        $token = $request->header('storetoken');
        $accountinfo = Redis::get($token);
        $accountinfo = $accountinfo ? json_decode($accountinfo, true) : '';

        if(!is_array($accountinfo)){
            return '';
        }

        $accountinfo['store_ids']=!empty($accountinfo['store_ids'])?explode(',', $accountinfo['store_ids']):[];

        if(count($accountinfo['store_ids'])<=1){
            $accountinfo['store_ids'] =$accountinfo['store_ids'][0]??0;
        }
        if ($key) {
            if (isset($accountinfo[$key])) {
                return $accountinfo[$key];
            } else {
                return '';
            }
        } else {
            return $accountinfo;
        }
    }


    /**
     * 存储管理员token集合
     */
    public static function storetokenkey($token,$accountid){
        $token_list=Redis::get('store_user_token_list_'.$accountid);
        $token_list_arr=$token_list?json_decode($token_list,true):[];
        $token_list_arr[]=$token;
        Redis::set('store_user_token_list_'.$accountid,json_encode($token_list_arr));
    }

    /**
     * 清除管理员token集合，常用语修改密码后
     */
    public static function cleartokenkey($accountid){
        $token_list=Redis::get('store_user_token_list_'.$accountid);
        $token_list_arr=$token_list?json_decode($token_list,true):[];
        foreach($token_list_arr as $val){
            Redis::delete($val);
        }
        Redis::delete('store_user_token_list_'.$accountid);

    }



    /**
     * 存储管理员token集合
     */
    public static function storetokenkeybywxapp($token,$accountid){
        $token_list=Redis::get('store_user_bywxapp_token_list_'.$accountid);
        $token_list_arr=$token_list?json_decode($token_list,true):[];
        $token_list_arr[]=$token;
        Redis::setex('store_user_bywxapp_token_list_'.$accountid,(strtotime(date('Y-m-d').' 23:59:59')-time()),json_encode($token_list_arr));
    }

    /**
     * 清除管理员token集合，常用语修改密码后
     */
    public static function cleartokenkeybywxapp($accountid){
        $token_list=Redis::get('store_user_bywxapp_token_list_'.$accountid);
        $token_list_arr=$token_list?json_decode($token_list,true):[];
        foreach($token_list_arr as $val){
            Redis::delete($val);
        }
        Redis::delete('store_user_bywxapp_token_list_'.$accountid);

    }

    /**
     * 获取公司培训上下限
     */
    public static function gettraining($request)
    {
        $cp_id = self::getTokenInfo($request, 'cp_id');
        $company = SiteCompany::where(['id' => $cp_id])->first();
        if ($company) {
            return [
                'training_min' => $company->training_min,
                'training_max' => $company->training_max,
            ];
        }
        return [
            'training_min' => 0,
            'training_max' => 0,
        ];
    }



    public static function createOrderSn($len = 2)
    {

        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        mt_srand(10000000 * (float)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; ++$i) {
            $str .= $chars[mt_rand(0, $lc)];
        }
        return (date('Y')-2000).date('mdHis').$str;
    }


    /**
     * 时间处理
     * @param $date
     * @return false|int|string
     */
    public function endTimeFormat($date)
    {
        if (is_numeric($date)) {
            $date = date("Y-m-d", $date);
            $date = sprintf('%s %s', $date, '23:59:59');
            $date = strtotime($date);
        } else {
            $date = sprintf('%s %s', $date, '23:59:59');
        }
        return $date;
    }
}
